//
//  MCTableViewController.m
//  HMmall
//
//  Created by 李李善 on 16/6/24.
//  Copyright © 2016年 吉青. All rights reserved.
//

#import "MCTableViewController.h"
#import "UITableView+HD_NoList.h"
#import <MJRefresh/MJRefresh.h>


@interface  MCTableViewController ()

@end

@implementation MCTableViewController
-(instancetype)initWithModel:(id)Model
{
    self = [super init];
    if (self) {
        self.model = Model;
    }
    return self;
}

- (instancetype)initWithId:(id)Id
{
    if (self = [super init]) {
        self.Id   = Id;
    }
    return self;
}

- (void)viewDidLoad {//自定义视图。
    [super viewDidLoad];
    
    
    [self onSetupBackButton];
    
    self.dataArr = [NSMutableArray array];
    
    self.isShowDataImage = YES;
    
    self.isFirstSection = YES;
    
    self.startIndex=self.currentPageIndex = 1;
    
    __weak typeof(self) weakSelf = self;
    [self.tableView config:^(UITableView *tableView) {
        tableView.backgroundColor=[UIColor whiteColor];
        tableView.tableFooterView = [UIView new];
        if (weakSelf.isAddMJRefreshHeader ==YES) {
            tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [weakSelf onLoadDataForPageNum:weakSelf.startIndex];
            }];
        }
        if (weakSelf.isAddMJRefreshFooder==YES) {
            tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                [weakSelf onLoadDataForPageNum:weakSelf.currentPageIndex];
            }];
        }
    }];
    
    
    self.insertFrontSection = 0.f;
    self.insertBackSection = 0.f;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MCCellInfo *info =[self tableView:tableView cellForIndexPath:indexPath IsHeight:YES];
    if (info==nil) {
        return CGFLOAT_MIN;
    }
    return  info.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MCCellInfo *info =[self tableView:tableView cellForIndexPath:indexPath IsHeight:NO];
    
    if (info.cellId == nil) {
        return nil;
    }
    BaseTableViewCell * tableViewCell = [tableView dequeueReusableCellWithIdentifier:info.cellId];
    if (!tableViewCell) {
        
        if ([info.aClass isSubclassOfClass:[NSNull class]]) {
            return nil;
        }
        __weak UIViewController * vc = self;
        tableViewCell = [[info.aClass alloc]initWithStyle:info.cellStyle forViewController:vc reuseIdentifier:info.cellId forDataSource:[self cellInitForSourceAtIndexPath:indexPath] forIndexPath:indexPath];
        if (info.initBlock) {
        info.initBlock(tableViewCell,indexPath);
        }
        [self onSetSubviewSELForCell:tableViewCell forTableView:tableView forIndexPath:indexPath];
    }
    
    return tableViewCell;
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self onUpdateCell:cell model:[self modelForIndexPath:indexPath] atIndexPath:indexPath];
}




#pragma mark -----Header
- (nullable UIView *)tableView:(BaseTableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MCHFViewInfo *viewInfo = [self tableView:tableView HeaderInSection:section isBiao:NO];
    if (viewInfo==nil) {
        return nil;
    }
    BaseTableViewHeaderFooterView *View = [tableView dequeueReusableHeaderFooterViewWithIdentifier:viewInfo.viewId];
    if (!View) {
        if ([viewInfo.aClass isSubclassOfClass:[NSNull class]]) {
            return nil;
        }
        View = [[viewInfo.aClass alloc]initWithViewController:self reuseIdentifier:viewInfo.viewId forDataSource:[self headerInitForSourceAtSection:section] AtSection:section];
        [self onSetSubviewSELForviewHeader:View forTableView:tableView forSection:section];
    }
    return View;
}

-(CGFloat)tableView:(BaseTableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    MCHFViewInfo *viewInfo = [self tableView:tableView HeaderInSection:section isBiao:YES];
    if (viewInfo==nil) {
        return CGFLOAT_MIN;
    }
    return viewInfo.viewHeight;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(BaseTableViewHeaderFooterView *)view forSection:(NSInteger)section
{
    [self onUpdateHeaderView:view model:[self headerViewModelForSection:section] atSection:section];
}
- (void)onUpdateHeaderView:(BaseTableViewHeaderFooterView *_Nullable )view model:(id _Nullable)model atSection:(NSInteger)section
{
    __weak MCTableViewController *vc = self;
    [view update:model forTableView:vc.tableView AtSection:section];
}

#pragma mark -----Footer
- (nullable UIView *)tableView:(BaseTableView *)tableView viewForFooterInSection:(NSInteger)section
{
    MCHFViewInfo *viewInfo = [self tableView:tableView FooderInSection:section isBiao:NO];
    
    BaseTableViewHeaderFooterView *View = [tableView dequeueReusableHeaderFooterViewWithIdentifier:viewInfo.viewId];
    if (!View) {
        if ([viewInfo.aClass isSubclassOfClass:[NSNull class]]) {
            return nil;
        }
        View = [[viewInfo.aClass alloc]initWithViewController:self reuseIdentifier:viewInfo.viewId forDataSource:[self fooderInitForSourceAtSection:section] AtSection:section];
        [self onSetSubviewSELForViewFooder:View forTableView:tableView forSection:section];
    }
    
    return View;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    MCHFViewInfo *viewInfo = [self tableView:(id)tableView FooderInSection:section isBiao:YES];
    if (viewInfo==nil) {
        return CGFLOAT_MIN;
    }
    return viewInfo.viewHeight;
}
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(BaseTableViewHeaderFooterView *)view forSection:(NSInteger)section
{
    [self onUpdateFooderView:view model:[self fooderViewModelForSection:section] atSection:section];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseTableViewCell *cell = (BaseTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (cell.cellBlcok) {
        cell.cellBlcok(cell,indexPath,cell.model);
    }
    
}


@end
